/**
 * *******************************************
 * user: zmq
 * describe:    自定义路由模块
 * created:
 * date: 2021/8/12
 * *******************************************
 */

import {FrontendSession, Application} from "pinus";
import Dispatcher from "./dispatcher";

export default class RouteUtil{

    /**
     * area 区服路由
     * @param {FrontendSession} session
     * @param msg
     * @param {Application} app
     * @param cb
     * @returns {any}
     */
    public static area(session: FrontendSession, msg: any, app: Application, cb) {

        console.log("session   = " + session);

        if (!session.frontendId) {       // 游戏id
            cb('fail to find frontend id  in session',undefined);
        }

        let areaServers = app.getServersByType('area');// 根据类型 获取服务器列表
        if (!areaServers || areaServers.length === 0) {// 如果服务器列表不存在或为空，则调用回调函数cb，将错误传给该回调
            cb('can not find conne servers or uid is undefined',undefined);
        }

        let res = Dispatcher.dispatch(session.frontendId, areaServers);   // 负载均衡
        console.log("area服务器：" + res.id);
        console.log("area.length       ", areaServers.length);
        cb(undefined, res.id);
    }
}